1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.io;
18
19 import com.google.common.collect.ImmutableSet;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStream;
24
25
26
27
28
29
30 public class TestByteSink extends ByteSink implements TestStreamSupplier {
31
32 private final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
33 private final ImmutableSet<TestOption> options;
34
35 private boolean outputStreamOpened;
36 private boolean outputStreamClosed;
37
38 public TestByteSink(TestOption... options) {
39 this.options = ImmutableSet.copyOf(options);
40 }
41
42 byte[] getBytes() {
43 return bytes.toByteArray();
44 }
45
46 @Override
47 public boolean wasStreamOpened() {
48 return outputStreamOpened;
49 }
50
51 @Override
52 public boolean wasStreamClosed() {
53 return outputStreamClosed;
54 }
55
56 @Override
57 public OutputStream openStream() throws IOException {
58 outputStreamOpened = true;
59 bytes.reset();
60 return new Out();
61 }
62
63 private final class Out extends TestOutputStream {
64
65 public Out() throws IOException {
66 super(bytes, options);
67 }
68
69 @Override
70 public void close() throws IOException {
71 outputStreamClosed = true;
72 super.close();
73 }
74 }
75 }